﻿using Md.Api.Entity.ModelDB;
using PetaPoco.NetCore;

namespace Md.Petapoco.Repositories.MdTrainingEducation;
/// <summary>
/// 老师/讲师仓储
/// </summary>
public class MdTeacherRep : BaseRep<MdTeacherModel>
{
    /// <summary>
    /// 老师/讲师登录
    /// </summary>
    /// <param name="account">老师/讲师账号</param>
    /// <param name="pwd">老师/讲师密码</param>
    /// <returns>MdTeacherModel</returns>
    public MdTeacherModel TeacherLogin(string? account, string? pwd)
    {
        #region ===sql===
        string sql = string.Format(@"
            select teacher_id
                ,teacher_name
                ,teacher_avatar
                ,teacher_desc
                ,teacher_intor
                ,teacher_account
                ,teacher_phone
                ,teacher_age
                ,teacher_sex
                ,teacher_idcard
                ,status
                ,del_flag
                ,remark
                ,create_by
                ,create_time
                ,update_by
                ,update_time
            from md_teacher
            where 1 = 1 and teacher_account=@0 and teacher_pwd_md5=@1 
         ");
        #endregion

        return MdTrainingEducationDB.GetInstance().SingleOrDefault<MdTeacherModel>(sql, account, pwd);
    }
    /// <summary>
    /// 通过编号获取老师/讲师信息
    /// </summary>
    /// <param name="teacherId">老师/讲师编号</param>
    /// <returns>MdTeacherModel</returns>
    public MdTeacherModel GetMdTeacherByTeacherId(long? teacherId)
    {
        #region ===sql===
        string sqlStr = string.Format(@"
            select teacher_id
                ,teacher_name
                ,teacher_avatar
                ,teacher_desc
                ,teacher_intor
                ,teacher_account
                ,teacher_phone
                ,teacher_age
                ,teacher_sex
                ,teacher_idcard
                ,status
                ,del_flag
                ,remark
                ,create_by
                ,create_time
                ,update_by
                ,update_time
            from md_teacher
            where 1 = 1 and teacher_id = @0
        ");
        #endregion

        return MdTrainingEducationDB.GetInstance().SingleOrDefault<MdTeacherModel>(sqlStr, teacherId);
    }
    /// <summary>
    /// 获取老师/讲师列表数据
    /// </summary>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="model">筛选条件</param>
    /// <returns>IEnumerable<MdTeacherModel></returns>
    public IEnumerable<MdTeacherModel> GetMdTeacherList(string? orderBy, bool? isDesc, MdTeacherModel model)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.TeacherId > 0)
        {
            sqlWhere += " and teacher_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.TeacherName))
        {
            sqlWhere += " and teacher_name @1 ";
        }
        if (!string.IsNullOrWhiteSpace(model.TeacherAccount))
        {
            sqlWhere += " and teacher_account @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.TeacherPhone))
        {
            sqlWhere += " and teacher_phone @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.TeacherSex))
        {
            sqlWhere += " and teacher_sex = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.TeacherIdcard))
        {
            sqlWhere += " and teacher_idcard @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @6 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DelFlag))
        {
            sqlWhere += " and del_flag = @7 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @8 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @9 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select teacher_id
                ,teacher_name
                ,teacher_avatar
                ,teacher_desc
                ,teacher_intor
                ,teacher_account
                ,teacher_phone
                ,teacher_age
                ,teacher_sex
                ,teacher_idcard
                ,status
                ,del_flag
                ,remark
                ,create_by
                ,create_time
                ,update_by
                ,update_time
            from md_teacher
            where 1 = 1 {0}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Query<MdTeacherModel>(
            sqlStr,
            model.TeacherId,
            string.Format(@"%{0}%", model.TeacherName),
            string.Format(@"%{0}%", model.TeacherAccount),
            string.Format(@"%{0}%", model.TeacherPhone),
            model.TeacherSex,
            string.Format(@"%{0}%", model.TeacherIdcard),
            model.Status,
            model.DelFlag,
            model.CreateBy,
            model.UpdateBy);
    }
    /// <summary>
    /// 分页获取老师/讲师列表数据
    /// </summary>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="model">筛选条件</param>
    /// <returns>Page<MdTeacherModel></returns>
    public Page<MdTeacherModel> GetMdTeacherPageList(string? orderBy, bool? isDesc, int pageIndex, int pageSize, MdTeacherModel model)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.TeacherId > 0)
        {
            sqlWhere += " and teacher_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.TeacherName))
        {
            sqlWhere += " and teacher_name @1 ";
        }
        if (!string.IsNullOrWhiteSpace(model.TeacherAccount))
        {
            sqlWhere += " and teacher_account @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.TeacherPhone))
        {
            sqlWhere += " and teacher_phone @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.TeacherSex))
        {
            sqlWhere += " and teacher_sex = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.TeacherIdcard))
        {
            sqlWhere += " and teacher_idcard @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @6 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DelFlag))
        {
            sqlWhere += " and del_flag = @7 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @8 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @9 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select teacher_id
                ,teacher_name
                ,teacher_avatar
                ,teacher_desc
                ,teacher_intor
                ,teacher_account
                ,teacher_phone
                ,teacher_age
                ,teacher_sex
                ,teacher_idcard
                ,status
                ,del_flag
                ,remark
                ,create_by
                ,create_time
                ,update_by
                ,update_time
            from md_teacher
            where 1 = 1 {0}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Page<MdTeacherModel>(
            pageIndex,
            pageSize,
            sqlStr,
            model.TeacherId,
            string.Format(@"%{0}%", model.TeacherName),
            string.Format(@"%{0}%", model.TeacherAccount),
            string.Format(@"%{0}%", model.TeacherPhone),
            model.TeacherSex,
            string.Format(@"%{0}%", model.TeacherIdcard),
            model.Status,
            model.DelFlag,
            model.CreateBy,
            model.UpdateBy);
    }
    /// <summary>
    /// 通过编号更新老师/讲师信息
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateMdTeacherByTeacherId(MdTeacherModel model)
    {
        var setList = new List<string>();

        #region ===set===
        setList.Add(" teacher_name = @2 ");

        setList.Add(" teacher_avatar = @3 ");

        setList.Add(" teacher_desc = @4 ");

        setList.Add(" teacher_intor = @5 ");

        setList.Add(" teacher_account = @6 ");

        setList.Add(" teacher_pwd = @7 ");

        setList.Add(" teacher_pwd_md5 = @8 ");

        setList.Add(" teacher_phone = @9 ");

        setList.Add(" teacher_age = @10 ");

        setList.Add(" teacher_sex = @11 ");

        setList.Add(" teacher_idcard = @12 ");

        setList.Add(" status = @13 ");

        setList.Add(" update_by = @14 ");

        setList.Add(" update_time = @15 ");

        setList.Add(" remark = @16 ");
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
                set {0}
                where 1 = 1 and del_flag = @0 and teacher_id = @1
         ", string.Join(",", setList));
        #endregion

        var res = MdTrainingEducationDB.GetInstance().Update<MdTeacherModel>(
            sqlStr,
            model.DelFlag,
            model.TeacherId,
            model.TeacherName,
            model.TeacherAvatar,
            model.TeacherDesc,
            model.TeacherIntor,
            model.TeacherAccount,
            model.TeacherPwd,
            model.TeacherPwdMd5,
            model.TeacherPhone,
            model.TeacherAge,
            model.TeacherSex,
            model.TeacherIdcard,
            model.Status,
            model.UpdateBy,
            model.UpdateTime,
            model.Remark
         );

        return res;
    }
}

